/**
 * 作者 ：食草狂魔
 *
 * 日期 ：2017/11/13
 *
 * 描述 ：路由配置
 */
import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/pages/home/index.vue'
import templateManageMain from '@/pages/templateManage/main.vue'
import catalogManageMain from '@/pages/catalogManage/main.vue'
import resources from '@/pages/catalogManage/resources.vue'
import term from '@/pages/catalogManage/term.vue'
import editResources from '@/pages/catalogManage/editResources.vue'
import catalogAuditingMain from '@/pages/catalogAuditing/main.vue'
import cataDetails from '@/pages/catalogAuditing/cataDetails.vue'
import systemManageMain from '@/pages/systemManage/main.vue'
import systemManageDepartment from '@/pages/systemManage/department.vue'
import systemManageRole from '@/pages/systemManage/role.vue'
import statisticalMain from '@/pages/statistical/main.vue'
import searchMain from '@/pages/search/main.vue'
import helpcenterMain from '@/pages/helpcenter/main.vue'
import systemLogMain from '@/pages/systemLog/main.vue'
import login from '@/pages/login/login.vue'

import checkToken from './checkToken'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      // 首页
      path: '/',
      name: 'home-index',
      component: Index
    },
    {
      // 模板管理
      path: '/templateManage/main',
      name: 'templateManage-main',
      component: templateManageMain
    },
    {
      // 目录管理
      path: '/catalogManage/main',
      name: 'catalogManage-main',
      component: catalogManageMain
    },
    {
      // 目录管理--编辑信息资源
      path: '/catalogManage/editResources',
      name: 'editResources',
      component: editResources
    },
    {
      // 目录管理--修改信息资源
      path: '/catalogManage/resources',
      name: 'resources',
      component: resources
    },
    {
      // 目录管理--修改信息项
      path: '/catalogManage/term',
      name: 'term',
      component: term
    },
    {
      // 目录审核
      path: '/catalogAuditing/main',
      name: 'catalogAuditing-main',
      component: catalogAuditingMain
    },
    {
      // 目录审核--详情页
      path: '/catalogAuditing/cataDetails',
      name: 'cataDetails',
      component: cataDetails
    },
    {
      // 系统管理 - 用户管理
      path: '/systemManage/main',
      name: 'systemManage-main',
      component: systemManageMain
    },
    {
      // 系统管理 - 部门管理
      path: '/systemManage/department',
      name: 'systemManage-department',
      component: systemManageDepartment
    },
    {
      // 系统管理 - 角色管理
      path: '/systemManage/role',
      name: 'systemManage-role',
      component: systemManageRole
    },
    {
      // 统计分析
      path: '/statistical/main',
      name: 'statistical-main',
      component: statisticalMain
    },
    {
      // 搜索
      path: '/search/main',
      name: 'search-main',
      component: searchMain
    },
    {
      // 帮助中心
      path: '/helpcenter/main',
      name: 'helpcenter-main',
      component: helpcenterMain
    },
    {
      // 日志
      path: '/systemLog/main',
      name: 'systemLog-main',
      component: systemLogMain
    },
    {
      // 登录
      path: '/login',
      name: 'login',
      component: login
    }
  ]
})

router.beforeEach((to, from, next) => {
  let allow = false
  let partten = ['^/login']
  partten.forEach(item => {
    // item里都是非受限模块
    var reg = new RegExp(item)
    if (reg.test(to.fullPath)) {
      allow = true
    }
  })

  if(allow){
    // console.log(`不需要鉴权`)
    next()
  }
  else{
    // console.log(`需要鉴权`, to.fullPath)
    checkToken(to, from, next)
    // next()
  }
})

export default router
